<? // save

$ud_id = @pure_integer($_GET['ud_id']);
$title = pure_string($_GET['title']);
$url = pure_url($_GET['url']);
$data = pure_string($_POST['data']);
$usr_id = $_SESSION['usr_id'];

//-$buf = '';
//-foreach( $_POST as $key=>$val ) $buf .= 'POST['.$key.']='.substr($val,0,5).' ';
//-echo "post = $buf";
//-exit;

// see if we can update
if( !$ud_id ) $ud_id = db_get_col( "select ud_id from user_data where usr_id=$usr_id and url='$url' and title='$title'" );

// if existent record, update
if( $ud_id ) {
	$query = "update user_data set
			url=".(($url)?"'$url'":'null').",
			title='$title',
			access_ct=access_ct+1,
			access_ts=current_timestamp,
			data='$data'
		where ud_id = $ud_id";
	//-echo "update: $query<br/>";
	$result = db_dml( $query );
	if( !$result ) ajax_exit( 'error', 'unable to update data record' );
}

// if new record, insert
else {
	$query = "insert into user_data( usr_id, url, title, data ) values( $usr_id, ".(($url)?"'$url'":'null').", '$title', '$data' )";
	//-echo "insert: $query<br/>";
	$result = db_dml( $query );
	if( !$result ) ajax_exit( 'error', 'unable to insert data record' );
	$ud_id = mysql_insert_id();
}

ajax_exit( 'success', 'data updated', array( 'ud_id'=>$ud_id ) );

?>
